package com.google.project.client.Director;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.project.client.Service.LoginService;
import com.google.project.client.Service.LoginServiceAsync;
import com.google.project.client.Sharing.ChangePassword;
import com.google.project.shared.UsersDTO;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Hyperlink;

public class DRTAction extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField Button btnProjectReport;
	@UiField Button btnMessage;
	@UiField Button btnInfo;
	@UiField Label lblFullName;
	@UiField Hyperlink hypSingOut;
	@UiField Button btnHMReq;
	@UiField Hyperlink btnChangePass;
	

	interface Binder extends UiBinder<Widget, DRTAction> {
	}
	
	private final LoginServiceAsync loginService = GWT
	.create(LoginService.class);
	UsersDTO userDTO=new UsersDTO();
	public DRTAction() {
		initWidget(binder.createAndBindUi(this));
		getUserByID();
	}

	@UiHandler("btnInfo")
	void onBtnAllInfoClick(ClickEvent event) {
		DRTEntrypoint.get().DRTHomepage();
	}
	
	@UiHandler("btnListProject")
	void onBtnListProjectClick(ClickEvent event) {
		DRTEntrypoint.get().DRTProjectList();
	}
	
	@UiHandler("btnProjectReport")
	void onButtonClick(ClickEvent event) {
		DRTEntrypoint.get().DRTProjectReportList();
	}
	
	@UiHandler("btnMessage")
	void onBtnMessageClick(ClickEvent event) {
		DRTEntrypoint.get().DRTMessList();
	}
	
	@UiHandler("btnHMReq")
	void onBtnHMReqClick(ClickEvent event) {
		DRTEntrypoint.get().DRTHumanReq();
	}
	
	public void getUserByID() {
		loginService.getCurrentUserDTO(new AsyncCallback<UsersDTO>() {
			
			@Override
			public void onSuccess(UsersDTO result) {
				userDTO=result;
				lblFullName.setText("Xin chào: " + result.getFullName());
			}
			
			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}
	@UiHandler("hypSingOut")
	void onHypSingOutClick(ClickEvent event) {
		loginService.logout(new AsyncCallback<Void>() {

			@Override
			public void onSuccess(Void result) {
				Window.Location.reload();
			}

			@Override
			public void onFailure(Throwable caught) {
			//	Window.alert("RPC có lỗi ");
			}
		});
	}
	@UiHandler("btnChangePass")
	void onBtnChangePassClick(ClickEvent event) {
		ChangePassword dialog=new ChangePassword(userDTO);
		dialog.setPopupPosition(350, 120);
		dialog.show();
	}
}
